package com {
	import com.bottomnavbar.BottomNavBar;
	import com.greensock.TweenMax;
	import com.itemOrder.ItemOrder;
	import com.itemstodisplay.ItemsToDisplay;
	import com.maincontainer.MainContainer;
	import com.midcontainer.ArrowButtonHolder;
	import com.midcontainer.MainBoxes;
	import com.totalitems.TotalItems;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class HomeWork extends Sprite {
		private var _mainContainer : MainContainer;
		private var _arrowButtonHolder : ArrowButtonHolder;
		private var _bottomNav : BottomNavBar;
		private var _totalItems : TotalItems;
		private var _itemsToDisplay : ItemsToDisplay;
		private var _itemOrder : ItemOrder;
		private var _boxes : MainBoxes;
		private var _countBox : int;

		// private var _boxStartX : Number;
		public function HomeWork() {
			init();
		}

		private function init() : void {
			_mainContainer = new MainContainer;
			addChild(_mainContainer);

			_boxes = new MainBoxes();
			addChild(_boxes);
			// _boxes.x = _mainContainer.height - _mainContainer.width / 2;
			// _boxes.y = _mainContainer.height - _mainContainer.height / 2;

			_arrowButtonHolder = new ArrowButtonHolder();
			_mainContainer.addChild(_arrowButtonHolder);
			_arrowButtonHolder.x = _mainContainer.width - _mainContainer.width / 2 - 108;
			_arrowButtonHolder.y = _mainContainer.height - _mainContainer.height / 2;

			_arrowButtonHolder._leftArrowBtn.addEventListener(MouseEvent.CLICK, onClickLeft);
			_arrowButtonHolder._rightArrowBtn.addEventListener(MouseEvent.CLICK, onClickRight);

			_bottomNav = new BottomNavBar();
			_mainContainer.addChild(_bottomNav);
			_bottomNav.x = _mainContainer.width - _mainContainer.width / 2 - 215;
			_bottomNav.y = _mainContainer.height - _mainContainer.height / 2 + 325;

			_totalItems = new TotalItems();
			_mainContainer.addChild(_totalItems);
			_totalItems.x = _mainContainer.width - _mainContainer.width / 2 - 500;
			_totalItems.y = _mainContainer.height - _mainContainer.height / 2 + 298;

			_itemOrder = new ItemOrder();
			_mainContainer.addChild(_itemOrder);
			_itemOrder.x = _mainContainer.width - _mainContainer.width / 2 - 340;
			_itemOrder.y = _mainContainer.height - _mainContainer.height / 2 + 300;

			_itemsToDisplay = new ItemsToDisplay();
			_mainContainer.addChild(_itemsToDisplay);
			_itemsToDisplay.x = _mainContainer.width - _mainContainer.width / 2 + 350;
			_itemsToDisplay.y = _mainContainer.height - _mainContainer.height / 2 + 300;

			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandle, false, 0, false);
		}

		private function addedToStageHandle(evt : Event) : void {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(Event.RESIZE, resizeHandler);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}

		public function resizeHandler(e : Event) : void {
			// _boxes.x = (_boxes.stage.stageWidth / 2) - (_boxes.width / 2);
			// _boxes.y = (_boxes.stage.stageHeight / 2) - (_boxes.height / 2);
			_mainContainer.x = (_mainContainer.stage.stageWidth / 2) - (_mainContainer.width / 2);
			_mainContainer.y = (_mainContainer.stage.stageHeight / 2) - (_mainContainer.height / 2);
		}

		private function onClickLeft(evt : Event) : void {
			var boxMove : Number = 310 + 132 * _countBox;
			TweenMax.to(_boxes, .5, {alpha:1, x:boxMove});

			_countBox++;
			updateBoxPosition();
		}

		private function onClickRight(evt : Event) : void {
			var boxMove : Number = 310 - 132 * _countBox;
			TweenMax.to(_boxes, .5, {alpha:1, x:boxMove});

			_countBox--;
			updateBoxPosition();
		}

		private function updateBoxPosition() : void {
			var boxMove : Number = 310 + 132 * _countBox;
			TweenMax.to(_boxes, .5, {alpha:1, x:boxMove});
		}
	}
}
